package com.techwells.applicationMarket.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techwells.applicationMarket.util.ResultInfo;

@ControllerAdvice
@ResponseBody
public class GlobalControllerHandler {
	
	private Logger log=LoggerFactory.getLogger(GlobalControllerHandler.class);
	
	/**
	 * 	处理运行时异常
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	public ResultInfo exceptionHanler(Exception ex) {
		ResultInfo resultInfo = new ResultInfo();
		if (ex instanceof GlobalException) {
			GlobalException exception=(GlobalException)ex;
			log.error(exception.getMessage(),exception);
			resultInfo.setCode(exception.getCode());
			resultInfo.setMessage(exception.getMessage());
			return resultInfo;
		}
		log.error("服务异常",ex);
		resultInfo.setCode("-1");
		resultInfo.setMessage("服务异常");
		return resultInfo;
	}
	
	
	
	/**
	 * 	自定义的全局异常处理器
	 * 	@param ex 异常
	 * @return
	 */
	@ExceptionHandler(value = GlobalException.class)
	public ResultInfo globalExceptionHandler(GlobalException ex) {
		ResultInfo resultInfo = new ResultInfo();
//		log.error(ex.getMessage(),ex);
		resultInfo.setCode(ex.getCode());
		resultInfo.setMessage(ex.getMessage());
		return resultInfo;
	}
	
}
